<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('Logo')" prop="productLogo">
            <UploadAvatar :logo-info="form.productLogo" @appendData="uploadImg($event)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('产品名称')" prop="productName">
            <el-input v-model="form.productName" :placeholder="$t('产品名称')" show-word-limit maxlength="127" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资金额')" prop="investMoney">
            <el-input v-model="form.investMoney" :placeholder="$t('投资金额')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资时间')" prop="investDate">
            <DatePicker
              v-model="form.investDate"
              :placeholder="$t('投资时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('新闻标题')" prop="newsTitle">
            <el-input v-model="form.newsTitle" :placeholder="$t('新闻标题')" show-word-limit maxlength="127" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('新闻url')" prop="newsUrl">
            <el-input v-model="form.newsUrl" :placeholder="$t('新闻url')" show-word-limit maxlength="127" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('轮次')" prop="investRound">
            <dictSelect
              v-model="form.investRound"
              :clearable="true"
              dictionary="rounds_investment"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="gp:investEvent" />
    </div>
  </div>
</template>

<script>
import {
  saveGpInvestEvent,
  getGpInvestEvent,
  uploadAvatar
} from '@/api/fund/gp/tycInvest/gpInvestEvent'
import UploadAvatar from '@/components/Avatar/uploadAvatar'
export default {
  components: {
    UploadAvatar
  },
  data() {
    return {
      // 表单参数
      form: {
        productLogo: undefined
      },
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        productName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        investMoney: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        investRound: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        investDate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data != null && data.id !== undefined) {
        getGpInvestEvent(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        gpId: this.$route.query.gpId,
        productName: undefined,
        productLogo: undefined,
        newsUrl: undefined,
        productId: undefined,
        investMoney: undefined,
        investRound: undefined,
        investDate: undefined,
        newsTitle: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.source = '1'
          saveGpInvestEvent(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 上传图片
    uploadImg(data) {
      const formData = new FormData()
      formData.append('avatarfile', data)
      formData.append('id', this.form.id)
      uploadAvatar(formData).then(response => {
        if (response.code === 200) {
          this.form.productLogo = response.msg
          this.msgSuccess(this.$t('上传成功'))
        } else {
          this.msgError(response.msg)
        }
      })
    }
  }

}
</script>
